package ywpt.service.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientInfo;
import ywpt.model.ClientLinkman;
import ywpt.model.CommonTable;
import ywpt.web.common.PublicFunction;

public class ClientLinkmanServiceImpl  extends HibernateEntityDao<ClientLinkman> implements ClientLinkmanService{
	public ClientLinkmanServiceImpl(){
		
	}

	@Override
	public ClientLinkman getClientLinkmanById(Long id) {
		return this.get(id);
	}

	@Override
	public void removeClientLinkman(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveClientLinkman(ClientLinkman clientLinkman, Long clientId,
			Long typeId) {
		if(clientId != null){
			clientLinkman.setClient(this.get(ClientInfo.class, clientId));
		}
		if(typeId !=null ){
			clientLinkman.setType(this.get(CommonTable.class, typeId));
		}
		super.save(clientLinkman);
	}

	@Override
	public void updateClientLinkman(ClientLinkman clientLinkman, Long clientId,
			Long typeId) {
		if(clientId != null){
			clientLinkman.setClient(this.get(ClientInfo.class, clientId));
		}
		if(typeId !=null ){
			clientLinkman.setType(this.get(CommonTable.class, typeId));
		}
		super.update(clientLinkman);
	}

	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ClientLinkman> getClientLinkmanByManName(String manName,
			Long clientId, int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientLinkman.class, "linkman");
		query.add(Restrictions.eq("client", this.get(ClientInfo.class, clientId)));
		if ( manName != null && !manName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + manName + "%"),  
							          Restrictions.like("remark", "%" + manName + "%")));
		}
		query.addOrder(Order.asc("id"));
		List<ClientLinkman> clientLinkmanList = new ArrayList<ClientLinkman>();
		List<ClientLinkman> clientLinkmanLists = new ArrayList<ClientLinkman>();
		ClientLinkman linkman =new ClientLinkman();
		
		clientLinkmanList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ClientLinkman> itr = clientLinkmanList.iterator(); itr.hasNext(); i++ ) 
		{
			linkman = itr.next();
			String ss = PublicFunction.format("", 6, "  ", 1)
			          + linkman.getName(); 
			
			linkman.setName(ss);
			clientLinkmanLists.add(linkman);
		}
		return clientLinkmanLists;
	}

	@Override
	public int getClientLinkmanCountByName(String manName, Long clientId) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientLinkman.class, "linkman");
		query.add(Restrictions.eq("client", this.get(ClientInfo.class, clientId)));
		if ( manName != null && !manName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + manName + "%" ),  
							          Restrictions.like("remark", "%" + manName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientLinkman> getClientLinkmans(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}
}
